// =========== js小数计算
var HcNumber = {
    /**
     * 加法运算，解决js小数运算精度不准问题
     */
    add: function (num1, num2, scale) {
        var r1, r2, m, num;
        try {
            r1 = num1.toString().split('.')[1].length;
        } catch (e) {
            r1 = 0;
        }
        try {
            r2 = num2.toString().split('.')[1].length;
        } catch (e) {
            r2 = 0;
        }

        m = Math.pow(10, Math.max(r1, r2));
        num = (num1 * m + num2 * m) / m;
        if (scale == undefined || scale == null) {
            return num;
        } else {
            return num.toFixed(scale);
        }
    },
    /**
     * 减法运算，解决js小数运算精度不准问题
     */
    sub: function (num1, num2, scale) {
        var r1, r2, m, num;
        try {
            r1 = num1.toString().split('.')[1].length;
        } catch (e) {
            r1 = 0;
        }
        try {
            r2 = num2.toString().split('.')[1].length;
        } catch (e) {
            r2 = 0;
        }

        m = Math.pow(10, Math.max(r1, r2));
        num = (num1 * m - num2 * m) / m;
        if (scale == undefined || scale == null) {
            return num;
        } else {
            return num.toFixed(scale);
        }
    },
    /**
     * 乘法运算，解决js小数运算精度不准问题
     */
    mul: function (num1, num2, scale) {
        var s1, s2, m = 0, num;
        try {
            s1 = num1.toString()
            m += s1.split('.')[1].length;
        } catch (e) {
        }

        try {
            s2 = num2.toString()
            m += s2.split('.')[1].length;
        } catch (e) {
        }

        num = (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m);
        if (scale == undefined || scale == null) {
            return num;
        } else {
            return num.toFixed(scale);
        }
    },
    /**
     * 除法运算，解决js小数运算精度不准问题
     */
    div: function (num1, num2, scale) {
        var r1, r2, s1, s2;
        try {
            s1 = num1.toString();
            r1 = s1.split('.')[1].length;
        } catch (e) {
            r1 = 0;
        }
        try {
            s2 = num2.toString();
            r2 = s2.toString().split('.')[1].length;
        } catch (e) {
            r2 = 0;
        }

        num = (Number(s1.replace('.', '')) / Number(s2.replace('.', ''))) / Math.pow(10, r1 - r2);
        if (scale == undefined || scale == null) {
            return num;
        } else {
            return num.toFixed(scale);
        }
    }
};